package com.guanghua.brick.html;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guanghua.brick.html.tag.WebAppContextTag;
import com.guanghua.brick.util.StringUtil;

public class BindControl extends WebControl {
	
	//private static Log logger = LogFactory.getLog(InputControl.class);
	
	private String url = null;
	private String optionName = "checkbox";
	private String windowName = "window";
	private int windowWidth = 800;
	private int windowHeight = 600;
	private boolean modalDialog = false;
	
	private List<String> currentPageField = new ArrayList<String>();
	private List<String> optionAttributeName = new ArrayList<String>();
	
	@Override
	public String buildHTML(HttpServletRequest request, HttpServletResponse response) {
		StringBuffer html = new StringBuffer();
		
		html.append("<img id=\"")
		.append(this.getId())
		.append("\" src=\"")
		.append(WebAppContextTag.getWebAppContext(request))
		.append("/brick/image/bind.gif\" class=\"img_button\" url=\"")
		.append(WebAppContextTag.getWebAppContext(request))
		.append(this.url)
		.append("\" parentFieldName=\"")
		.append(StringUtil.stringArrayToString(currentPageField.toArray(new String[0])))
		.append("\" selectFieldName=\"")
		.append(StringUtil.stringArrayToString(optionAttributeName.toArray(new String[0])))
		.append("\" optionName=\"")
		.append(this.optionName)
		.append("\" onClick=\"openBind('")
		.append(this.getId())
		.append("', '")
		.append(this.windowName)
		.append("', ")
		.append(this.windowWidth)
		.append(", ")
		.append(this.windowHeight)
		.append(", ")
		.append(this.modalDialog)
		.append(");return false;\" />\n");
		
		//scripts生成
		html.append("<script language=\"javascript\">\n");
		html.append("	var ")
			.append(this.getId())
			.append(" = new Bind(\"")
			.append(this.getId())
			.append("\");\n");
		
		//生成depend
		if (this.getDepends() != null) {
			Iterator<IDepend> i = this.getDepends().iterator();
			while (i.hasNext()) {
				html.append("	")
				.append(i.next().buildScripts(this.getId()))
				.append("\n");
			}
		}
		
		html.append("</script>");
		
		return html.toString();
	}

	public boolean isModalDialog() {
		return modalDialog;
	}

	public void setModalDialog(boolean modalDialog) {
		this.modalDialog = modalDialog;
	}

	public String getOptionName() {
		return optionName;
	}

	public void setOptionName(String optionName) {
		this.optionName = optionName;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getWindowHeight() {
		return windowHeight;
	}

	public void setWindowHeight(int windowHeight) {
		this.windowHeight = windowHeight;
	}

	public String getWindowName() {
		return windowName;
	}

	public void setWindowName(String windowName) {
		this.windowName = windowName;
	}

	public int getWindowWidth() {
		return windowWidth;
	}

	public void setWindowWidth(int windowWidth) {
		this.windowWidth = windowWidth;
	}

	public void addBindMap(String currentPageField, String optionAttributeName) {
		this.currentPageField.add(currentPageField);
		this.optionAttributeName.add(optionAttributeName);
	}
}
